package cn.com.woxitech.dev.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.woxitech.dev.dao.gen.WmAppMapper;
import cn.com.woxitech.dev.dao.gen.WmCatalogMapper;
import cn.com.woxitech.dev.dao.gen.WmCatalogRoomMapper;
import cn.com.woxitech.dev.dao.gen.WmCatalogUserMapper;
import cn.com.woxitech.dev.dao.gen.WmLecturePageMapper;
import cn.com.woxitech.dev.dao.gen.WmSettingMapper;
import cn.com.woxitech.dev.model.gen.WmApp;
import cn.com.woxitech.dev.model.gen.WmAppExample;
import cn.com.woxitech.dev.model.gen.WmCatalog;
import cn.com.woxitech.dev.model.gen.WmCatalogExample;
import cn.com.woxitech.dev.model.gen.WmCatalogRoomExample;
import cn.com.woxitech.dev.model.gen.WmCatalogRoomKey;
import cn.com.woxitech.dev.model.gen.WmCatalogUserExample;
import cn.com.woxitech.dev.model.gen.WmCatalogUserKey;
import cn.com.woxitech.dev.model.gen.WmLecturePage;
import cn.com.woxitech.dev.model.gen.WmLecturePageExample;
import cn.com.woxitech.dev.model.gen.WmSetting;
import cn.com.woxitech.dev.model.gen.WmSettingExample;

@Service
public class SettingService {
	@Autowired
	WmSettingMapper wmSettingMapper;

	public String getSettingVal(String key){
		
		WmSettingExample e = new WmSettingExample();
		e.createCriteria().andMkeyEqualTo(key);
		List<WmSetting> ml = wmSettingMapper.selectByExample(e);
		
		if(ml == null || ml.isEmpty()){
			return null;
		}else{
			return ml.get(0).getMval();
		}
	}
	
	public void saveSettingVal(String key, String val){
		
		WmSettingExample e = new WmSettingExample();
		e.createCriteria().andMkeyEqualTo(key);
		List<WmSetting> ml = wmSettingMapper.selectByExample(e);
		
		WmSetting setting = new WmSetting();
		
		
		if(ml == null || ml.isEmpty()){
			setting.setMkey(key);
			setting.setMval(val);
			wmSettingMapper.insertSelective(setting);
			
		}else{
			setting = ml.get(0);
			setting.setMkey(key);
			setting.setMval(val);
			
			wmSettingMapper.updateByPrimaryKeySelective(setting);
		}
	}
}
